package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Test2 {

    public void test2() {
        System.out.println("test2 invoke...");
    }
    public static void main(String[] args) throws Exception {
        File dir = new File(Test2.class.getResource(".").toURI());
        File packageName = new File(Test2.class.getPackage().getName());
        File[] files = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File file : files) {
            String className = file.getName().split("\\.")[0];
            Class<?> clazz = Class.forName(packageName + "." + className);
            Object obj = clazz.newInstance();
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                if (method.getParameterCount() == 0 && Modifier.isPublic(method.getModifiers())) {
                    System.out.println(method.getName() + "执行了...");
                    method.invoke(obj);
                }
            }
        }
    }
}
